import { Component, Injector } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { UpdateAuditTimeInput, WorkFlowManagerServiceProxy } from '@shared/service-proxies/service-proxies';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { merge as _merge } from 'lodash-es';
import { finalize } from 'rxjs/operators';

@Component({
    templateUrl: './audit-time-edit-modal.component.html'
})
export class AuditTimeEditModelComponent extends AppComponentBase {

    callback: {(data:{[key:string]:any}):void};

    isShown = false;
    isInitialized = false;

    loading = false;
    saving = false;

    entry: any = {};

    constructor(
        injector: Injector,
        public bsModalRef: BsModalRef,
        private _workFlowManagerService: WorkFlowManagerServiceProxy
    ) {
        super(injector);
    }

    close(): void {
        this.bsModalRef.hide();
    }

    shown(entry: any): void {
        this.isShown = true;
        
        this.entry = JSON.parse(JSON.stringify(entry));

        this.isInitialized = true;
    }

    save() {
        this.saving = true;
        this._workFlowManagerService.updateAudiTime(
            new UpdateAuditTimeInput({ 
                id: this.entry.id, 
                startTime: this.entry.startTime, 
                markTime: this.entry.markedTime,
                finfishTime: this.entry.finishedTime }))
            .pipe(finalize(() => this.saving = false))
            .subscribe(result => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.callback(this.entry);
                this.close();
        });
    }
}
